home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / BackClock / sources / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-09  |  10.7 KB  |  338 lines

  1. /*****************************************************************************
  2.  * 
  3.  * Nom                          : utils.c
  4.  * desc                         : utilitaires projet
  5.  *
  6.  * version                      : $VER: utils.c 2.2ß4 (17.10.98)
  7.  *
  8.  * ver 2.1:   error handling
  9.  * ver 2.2:   some bug fixed
  10.  * ver 2.2ß2: stay in the background now
  11.  * ver 2.2ß3: LoadConf used a uninitialized variable (pointer to WBScreen)
  12.  * ver 2.3ß4: problem when notifyint was not present
  13.  *            draw or not the seconds arrow
  14.  *             
  15.  *****************************************************************************
  16.  */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <exec/exec.h>
  22. #include <exec/ports.h>
  23. #include <exec/io.h>
  24. #include <exec/tasks.h>
  25. #include <exec/libraries.h>
  26. #include <dos/datetime.h>
  27. #include <dos/dosextens.h>
  28. #include <workbench/workbench.h>
  29. #include <libraries/notifyintuition.h>
  30.  
  31. #include <proto/exec.h>
  32. #include <proto/intuition.h>
  33. #include <proto/dos.h>
  34. #include <proto/graphics.h>
  35. #include <proto/gadtools.h>
  36. #include <proto/icon.h>
  37. #include "utils.h"
  38. #include "timer.h"
  39. #include "tracewin.h"
  40. #include "notify.h"
  41. #include "partial.h"
  42. #include "conf.h"
  43. #include "obp.h"
  44.  
  45. struct Library * NotifyIntuitionBase ;
  46.  
  47.  
  48. char * ptrTime = NULL ;
  49. struct DateTime * dt = NULL ;
  50.  
  51. idWin * init(WBArg * appName) {
  52.   /* initalize the main window
  53.    * and the project
  54.    */
  55.   struct Screen  * WBScreen = NULL ;                 // lock on WB
  56.   idWin * prjWin = NULL ;                            // main prj
  57.   
  58.   
  59.   if ((prjWin = AllocVec(sizeof(idWin), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  60.     return(NULL) ;
  61.     
  62.     
  63.   if ((prjWin->date = AllocVec(sizeof(struct DateStamp), MEMF_PUBLIC)) == NULL) {
  64.     close(prjWin) ;
  65.     return(NULL) ;
  66.   }
  67.     
  68.   if ((prjWin->Notify = AllocVec(sizeof(struct IntNotifyRequest), MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {
  69.     close(prjWin) ;
  70.     return(NULL) ;
  71.   }
  72.   
  73.   
  74.   if ((NotifyIntuitionBase = OpenLibrary(NOTIFYINTUITIONNAME, NOTIFYINTUITION_VMIN)) != NULL) {
  75.         /* open the window
  76.          */
  77.     
  78.     WBScreen = LockPubScreen("Workbench") ;
  79.     top = WBScreen->BarHeight ; 
  80.     prjWin->wb   = WBScreen ;
  81.     LoadConf(prjWin) ;
  82.     init_bitmap(prjWin) ;
  83.     prjWin->win = OpenWindowTags(NULL, WA_Left,   prjWin->backWin.posX,
  84.                                        WA_Top,         prjWin->backWin.posY,
  85.                                        WA_Width,       prjWin->backWin.width,
  86.                                        WA_Height,      prjWin->backWin.height,
  87.                                        WA_IDCMP,       IDCMP,
  88.                                        WA_MinWidth,      50,
  89.                                        WA_MinHeight,   50,
  90.                                        WA_MaxHeight,   MAXH,
  91.                                        WA_MaxWidth,    MAXH,
  92.                                        WA_Flags,       WFLG,
  93.                                        WA_ScreenTitle, TXT_SCRTITLE,
  94.                                        WA_NewLookMenus, TRUE,
  95.                                        WA_PubScreenName, "Workbench", TAG_DONE) ;
  96.     
  97.     szgdg.Height = top - 1 ;
  98.     szgdg.NextGadget = &tagdg ;
  99.     tagdg.LeftEdge  = prjWin->win->Width /2 ;
  100.     tagdg.TopEdge   = prjWin->win->Height - 20 ;
  101.     tagdg.Width     = prjWin->win->Width /2 ;
  102.     tagdg.Height    = 20 ;
  103.   
  104.     AddGList(prjWin->win, &szgdg, 0, 2, NULL) ;
  105.    
  106.     RefreshGList(&szgdg, prjWin->win, NULL, -1) ;
  107.                                                                                              
  108.     if (prjWin->win == NULL)
  109.       return(NULL) ;
  110.    
  111.     if (prjWin->wb)
  112.       UnlockPubScreen(NULL, WBScreen) ;
  113.     
  114.                                                                                                 
  115.           // initialisation ...  (fenetre...)
  116.   }else {
  117.     ez_req("Backclock Error", "Couldn't open notifyintuition.library V2", "Quit", NULL ) ;
  118.     if (prjWin->Notify) FreeVec(prjWin->Notify) ;
  119.     prjWin->Notify = NULL ;
  120.     close(prjWin) ;
  121.     return(NULL) ;
  122.   }
  123.   
  124.   if (!initTimer(prjWin))   { close(prjWin) ; return(NULL) ;}
  125.   if (!startNotify(prjWin)) { close(prjWin) ; return(NULL) ;}
  126.   if ((ptrTime = AllocVec(LEN_DATSTRING, MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  127.   if ((dt =      AllocVec(sizeof(struct DateTime),  MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  128.   getDate(prjWin) ;
  129.   initwin(prjWin) ;
  130.   return prjWin ;
  131. }
  132.  
  133. void close(idWin * prj) {
  134.   int i;
  135.   if (prj) {                              // struct prj existe (<>0) 
  136.     endNotify(prj) ;
  137.     if (prj->win) CloseWindow(prj->win) ; // ferme la fenetre
  138.     free_twin(prj) ; 
  139.     if (prj->date) FreeVec(prj->date) ;   // libere datestamp 
  140.     if (prj->Notify) FreeVec(prj->Notify) ; // libere struct IntNotify
  141.     closeTimer(prj) ;
  142.     
  143.     if (NotifyIntuitionBase) CloseLibrary(NotifyIntuitionBase) ;
  144.     prj->wb = LockPubScreen("Workbench") ;
  145.     for(i = 0; i<NUM_COLORS; i++)
  146.       freePen(prj, prj->backWin.cmap[i].reg) ;
  147.     if (prj->wb) UnlockPubScreen(NULL, prj->wb) ;
  148.     if (ptrTime) FreeVec(ptrTime) ;
  149.     free_bitmap(prj) ;
  150.     FreeVec(prj) ;                        // libere la memoire
  151.     FreeVec(dt) ;
  152.   }
  153. }
  154. void getDate(idWin * prj) {
  155.   // remplit la structure (projet) avec la date
  156.    
  157.   DateStamp(prj->date) ;
  158.   if (ptrTime) {  
  159.     CopyMem(prj->date, &(dt->dat_Stamp), sizeof(struct DateStamp)) ;
  160.     dt->dat_Format = FORMAT_DOS ;
  161.     dt->dat_StrTime = ptrTime ;
  162.     DateToStr(dt) ;
  163.     //CopyMem(ptrTime, prj->datestr, LEN_DATSTRING) ;
  164.     DateToByte(ptrTime, prj) ;
  165.   
  166.   }
  167. }
  168.  
  169. void processwin(idWin* prj) {
  170.   /* variables
  171.    */
  172.   ULONG signals = NULL,                                                                          /* masque des signaux a attendre */
  173.         mask,
  174.         class,                                                                                                                   /* masque des signaux recus */
  175.         sigtimer ;
  176.         
  177.   BOOL  stop    = FALSE ;                                                                                                                /* vrai si ctrl c */
  178.   struct IntuiMessage * msg ;                     /* ptr sur message intuition */
  179.   struct MsgPort      * extPort ;
  180.   struct backMsg      * extmsg ;
  181.   
  182.   extPort = CreateMsgPort() ;
  183.   extPort->mp_Node.ln_Name = AllocVec(10, MEMF_PUBLIC) ;
  184.   CopyMem("backclock", extPort->mp_Node.ln_Name, 10) ;
  185.   AddPort(extPort) ;
  186.   
  187.   sigtimer =  (1<<(prj->treq->tr_node.io_Message.mn_ReplyPort->mp_SigBit)) ; 
  188.   /* signaux d'attentes
  189.    */
  190.   signals = SIGBREAKF_CTRL_C | 
  191.             (1<<(prj->notifyPort->mp_SigBit)) | 
  192.             (1<<(prj->win->UserPort->mp_SigBit)) | sigtimer|
  193.             (1<<(extPort->mp_SigBit)) ;
  194.   
  195.   runtimer(prj,1) ;
  196.   getDate(prj) ;
  197.   effacer(prj) ;
  198.   retracer(prj) ;
  199.   while(!stop) {
  200.     mask = Wait(signals) ;
  201.     if (mask & SIGBREAKF_CTRL_C) {
  202.       /* recu le signal CTRL C
  203.        */
  204.       stop=TRUE;
  205.     }
  206.     if (mask & (1<<(extPort->mp_SigBit))) {
  207.       // recu message prog externe
  208.       extmsg = (struct backMsg *)GetMsg(extPort) ;
  209.       switch(extmsg->Class) {
  210.         case BC_Quit:        // recu demande d'arret
  211.           stop=TRUE ;
  212.           extmsg->error = OK ;
  213.           break;
  214.       
  215.         case BC_GetPrj:      // recu demande d'envoi du projet
  216.           extmsg->ptrPrj = prj ;
  217.           extmsg->error = OK ;
  218.           break;
  219.       
  220.         case BC_RefreshColors:
  221.           setColors(prj) ;
  222.           break;
  223.       
  224.         case BC_Refresh:     // refresh window (redraw)
  225.           reinit_win(prj) ;
  226.           getDate(prj) ;
  227.           effacer(prj) ;
  228.           retracer(prj) ;
  229.           extmsg->error = OK ;
  230.           break;
  231.       
  232.         case BC_SaveConf:
  233.           SaveEnv(prj, TRUE) ;
  234.           if (prj->lastError) extmsg->error = prj->lastError ;
  235.           break;
  236.       
  237.         case BC_UseConf:
  238.           SaveEnv(prj, FALSE) ;
  239.           if (prj->lastError) extmsg->error = prj->lastError ;
  240.           break;
  241.       
  242.         case BC_SetWindow:
  243.           getDate(prj) ;
  244.           setWindow(prj) ;
  245.           
  246.           AbortIO((struct IORequest*)prj->treq) ;
  247.           WaitIO((struct IORequest*)prj->treq) ;
  248.           if(prj->backWin.drawsec) {
  249.             
  250.             runtimer(prj, 1) ;
  251.           }
  252.           extmsg->error = OK ;
  253.           break;
  254.         
  255.         default:
  256.           break;
  257.       }
  258.       if (extmsg) ReplyMsg((struct Message*)extmsg) ;
  259.     }
  260.     if (mask & (1<<(prj->notifyPort->mp_SigBit))) {
  261.       // attention fermeture du WB
  262.       if (partialClose(prj)) partialOpen(prj) ;
  263.     }
  264.     if (mask & 1<<(prj->win->UserPort->mp_SigBit)) {
  265.       /* recu message d'intuition
  266.        */
  267.       while((msg = GT_GetIMsg(prj->win->UserPort)) != NULL) {
  268.         /* retourne le message
  269.          */
  270.         class = msg->Class ;
  271.         ReplyMsg((struct Message*)msg) ;
  272.         if(class == IDCMP_CHANGEWINDOW) {
  273.           
  274.           RemoveGList(prj->win, &szgdg, 2) ;
  275.           szgdg.Width = prj->win->Width ;
  276.           tagdg.LeftEdge = prj->win->Width/2 ;
  277.           tagdg.TopEdge  = prj->win->Height -20 ;
  278.           tagdg.Width    = prj->win->Width /2 ;
  279.           prj->backWin.width = prj->win->Width ;
  280.           prj->backWin.height = prj->win->Height ;      
  281.           prj->backWin.posX = prj->win->LeftEdge ;
  282.           prj->backWin.posY = prj->win->TopEdge ;
  283.           setWindow(prj) ;
  284.           reinit_win(prj) ; 
  285.           AddGList(prj->win, &szgdg, 0, 2, NULL) ;
  286.           RefreshGList(&szgdg, prj->win, NULL, -1) ;
  287.  
  288.           SaveEnv(prj, TRUE) ;
  289.         }  
  290.       }          
  291.     }
  292.     if (CheckIO((struct IORequest*)prj->treq)) {
  293.       WaitIO((struct IORequest*)prj->treq) ;
  294.       AbortIO((struct IORequest*)prj->treq) ;
  295.       WaitIO((struct IORequest*)prj->treq) ;
  296.       getDate(prj) ;
  297.       if(prj->backWin.drawsec)
  298.         runtimer(prj, 1) ;
  299.       else
  300.         runtimer(prj, 60-(long)(prj->sec)) ;
  301.       effacer(prj) ;
  302.       retracer(prj) ;
  303.       //WindowToBack(prj->win) ;
  304.     }  
  305.  
  306.   }
  307.   AbortIO((struct IORequest*)prj->treq) ;
  308.   WaitIO((struct IORequest*)prj->treq) ;
  309.   RemPort(extPort) ;
  310.   FreeVec(extPort->mp_Node.ln_Name) ;
  311.   DeleteMsgPort(extPort) ;
  312.      
  313. }
  314.  
  315. void ez_req(UBYTE * title, UBYTE * body, UBYTE * gadg, APTR arglist ) {
  316.   struct EasyStruct * ez_rq ;
  317.   
  318.   if (ez_rq = AllocVec(sizeof(struct EasyStruct), MEMF_PUBLIC)) {
  319.     if (ez_rq->es_Title = AllocVec(strlen(title)+1, MEMF_PUBLIC)) {
  320.      strcpy(ez_rq->es_Title, title) ;
  321.      if (ez_rq->es_TextFormat = AllocVec(strlen(body)+1, MEMF_PUBLIC)) {
  322.       strcpy(ez_rq->es_TextFormat, body) ;
  323.       if (ez_rq->es_GadgetFormat = AllocVec(strlen(gadg)+1, MEMF_PUBLIC)) {
  324.        strcpy(ez_rq->es_GadgetFormat, gadg) ;
  325.        ez_rq->es_StructSize = sizeof(EasyStruct) ;
  326.        ez_rq->es_Flags = NULL ;
  327.        EasyRequestArgs(NULL, ez_rq, NULL, arglist) ;
  328.        FreeVec(ez_rq->es_GadgetFormat) ;
  329.       }
  330.       FreeVec(ez_rq->es_TextFormat) ;
  331.      }
  332.      FreeVec(ez_rq->es_Title) ;
  333.     }
  334.     FreeVec(ez_rq) ;
  335.   }
  336. }
  337.  
  338.